package com.yanfan.zutai.controller;

import com.yanfan.common.annotation.Log;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.domain.entity.SysUser;
import com.yanfan.common.core.page.TableDataInfo;
import com.yanfan.common.enums.BusinessType;
import com.yanfan.common.utils.poi.ExcelUtil;
import com.yanfan.system.service.ISysUserService;
import com.yanfan.zutai.aop.SysInitData;
import com.yanfan.zutai.aop.SysInitUpdateData;
import com.yanfan.zutai.aop.SysQueryData;
import com.yanfan.zutai.domain.SysUserWeixin;
import com.yanfan.zutai.service.ISysUserWeixinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 微信用户绑定Controller
 *
 * @author ruoyi
 * @date 2021-12-24
 */
@RestController
@RequestMapping("/zutai/sysUserWeixin")
public class SysUserWeixinController extends BaseController {
    @Autowired
    private ISysUserWeixinService sysUserWeixinService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询微信用户绑定列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysUserWeixin:list')")
    @GetMapping("/list")
    @SysQueryData
    public TableDataInfo list(SysUserWeixin sysUserWeixin) {
        startPage();
        List<SysUserWeixin> list = sysUserWeixinService.selectSysUserWeixinList(sysUserWeixin);
        for (int i = 0; i < list.size(); i++) {
            SysUserWeixin sysUserWeixinQuery = list.get(i);
            SysUser sysUser = sysUserService.selectUserById(sysUserWeixinQuery.getUserId());
            sysUserWeixinQuery.setUserName(sysUser.getUserName());
            sysUserWeixinQuery.setNickName(sysUser.getNickName());
        }
        return getDataTable(list);
    }

    /**
     * 导出微信用户绑定列表
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysUserWeixin:export')")
    @Log(title = "微信用户绑定", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @SysQueryData
    public AjaxResult export(SysUserWeixin sysUserWeixin) {
        List<SysUserWeixin> list = sysUserWeixinService.selectSysUserWeixinList(sysUserWeixin);
        ExcelUtil<SysUserWeixin> util = new ExcelUtil<SysUserWeixin>(SysUserWeixin.class);
        return util.exportExcel(list, "sysUserWeixin");
    }

    /**
     * 获取微信用户绑定详细信息
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysUserWeixin:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(sysUserWeixinService.selectSysUserWeixinById(id));
    }

    /**
     * 新增微信用户绑定
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysUserWeixin:add')")
    @Log(title = "微信用户绑定", businessType = BusinessType.INSERT)
    @PostMapping
    @SysInitData
    public AjaxResult add(@RequestBody SysUserWeixin sysUserWeixin) {
        return toAjax(sysUserWeixinService.insertSysUserWeixin(sysUserWeixin));
    }

    /**
     * 修改微信用户绑定
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysUserWeixin:edit')")
    @Log(title = "微信用户绑定", businessType = BusinessType.UPDATE)
    @PutMapping
    @SysInitUpdateData
    public AjaxResult edit(@RequestBody SysUserWeixin sysUserWeixin) {
        return toAjax(sysUserWeixinService.updateSysUserWeixin(sysUserWeixin));
    }

    /**
     * 删除微信用户绑定
     */
    //@PreAuthorize("@ss.hasPermi('ghxx:sysUserWeixin:remove')")
    @Log(title = "微信用户绑定", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysUserWeixinService.deleteSysUserWeixinByIds(ids));
    }
}
